package com.comm.service.imp;


import com.comm.dao.imp.ExpressCompanyMappingDao;
import com.comm.domain.bo.ExpressCompanyMappingBO;
import com.comm.entity.ExpressCompanyMapping;
import com.comm.entity.Page;
import com.comm.entity.User;
import com.comm.service.IExpressCompanyMappingService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 系统物流和平台关系映射
 *
 * @author fenglun
 */
@Service
public class ExpressCompanyMappingServiceImpl implements IExpressCompanyMappingService {
    @Resource
    ExpressCompanyMappingDao expressCompanyMappingDao;

    @Override
    public Map<Long, ExpressCompanyMapping> getExpressMappingMap(User user) {
        Map<String, Object> params = new HashMap<>();
        params.put("source", user.getSource());
        params.put("active",true);
        List<ExpressCompanyMapping> expressCompanyMappingList = expressCompanyMappingDao.getExpressCompanyMappingByExample(params);
        if (CollectionUtils.isEmpty(expressCompanyMappingList)) {
            return null;
        }
        Map<Long, ExpressCompanyMapping> mappingMap = expressCompanyMappingList.stream().collect(Collectors.toMap(ExpressCompanyMapping::getRelativeExpressCompanyId, ExpressCompanyMapping -> ExpressCompanyMapping));
        return mappingMap;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int logicDeleteById(Long id) {
        return expressCompanyMappingDao.logicDeleteById(id);

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addExpressCompanyMapping(ExpressCompanyMapping expressCompanyMapping) {
        expressCompanyMappingDao.insert(expressCompanyMapping);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int batchAdd(List<ExpressCompanyMapping> expressCompanyMappings) {
        return expressCompanyMappingDao.batchInsert(expressCompanyMappings);
    }

    @Override
    public int batchUpdate(List<ExpressCompanyMapping> expressCompanyMappings) {
        expressCompanyMappingDao.batchUpdate(expressCompanyMappings);
        return expressCompanyMappings.size();
    }

    @Override
    public int update(ExpressCompanyMapping expressCompanyMapping) {
        return expressCompanyMappingDao.update(expressCompanyMapping);
    }

    @Override
    public List<ExpressCompanyMapping> getListByExample(ExpressCompanyMapping expressCompanyMapping, Page page) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", expressCompanyMapping.getId());
        params.put("source", expressCompanyMapping.getSource());
        params.put("relativeExpressCompanyId", expressCompanyMapping.getRelativeExpressCompanyId());
        params.put("sysBizCode", expressCompanyMapping.getSysBizCode());
        params.put("sysBizName", expressCompanyMapping.getSysBizName());
        params.put("active", expressCompanyMapping.getActive());
        params.put("page",page);
        return expressCompanyMappingDao.getExpressCompanyMappingByExample(params);
    }

    @Override
    public Object addBatch(List<ExpressCompanyMappingBO> list) {
        List<ExpressCompanyMapping> companyMappingList = new ArrayList<>(list.size());
        for (ExpressCompanyMappingBO expressCompanyMappingBO : list) {
            ExpressCompanyMapping expressCompanyMapping = new ExpressCompanyMapping();
            BeanUtils.copyProperties(expressCompanyMappingBO, expressCompanyMapping);
            companyMappingList.add(expressCompanyMapping);
        }
        return expressCompanyMappingDao.batchInsert(companyMappingList);    }
}
